//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OrnsteinUhlenbeckProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (Double speed, Double vol, Microsoft::FSharp::Core::FSharpOption<Double>^ x0, Microsoft::FSharp::Core::FSharpOption<Double>^ level) : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
    try
    {
#ifdef HANDLE
        _phOrnsteinUhlenbeckProcess = NULL;
#endif
        QuantLib::Real _speed = (QuantLib::Real)ValueHelper::Convert (speed); //d
        QuantLib::Volatility _vol = (QuantLib::Volatility)ValueHelper::Convert (vol); //d
        QuantLib::Real _x0 = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (x0) ? (QuantLib::Real)ValueHelper::Convert (x0->Value) : 0.0); //4
        QuantLib::Real _level = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (level) ? (QuantLib::Real)ValueHelper::Convert (level->Value) : 0.0); //4
        _ppOrnsteinUhlenbeckProcess = new boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess> (new QuantLib::OrnsteinUhlenbeckProcess ( _speed,  _vol,  _x0,  _level ));
        SetStochasticProcess1D (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess>& childNative, Object^ owner) : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
#ifdef HANDLE
	_phOrnsteinUhlenbeckProcess = NULL;
#endif
	_ppOrnsteinUhlenbeckProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
}
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (QuantLib::OrnsteinUhlenbeckProcess& childNative, Object^ owner) : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
#ifdef HANDLE
	_phOrnsteinUhlenbeckProcess = NULL;
#endif
	_ppOrnsteinUhlenbeckProcess = new boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
    _OrnsteinUhlenbeckProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (COrnsteinUhlenbeckProcess^ copy) : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
#ifdef HANDLE
	_phOrnsteinUhlenbeckProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOrnsteinUhlenbeckProcess = new boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
    }
}
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (PLATFORM::Type^ t) : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
#ifdef HANDLE
	_phOrnsteinUhlenbeckProcess = NULL;
#endif
	if (!t->IsSubclassOf(COrnsteinUhlenbeckProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (QuantLib::Handle<QuantLib::OrnsteinUhlenbeckProcess>& childNative, Object^ owner)  : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
	_phOrnsteinUhlenbeckProcess = &childNative;
	_ppOrnsteinUhlenbeckProcess = &static_cast<boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
    _OrnsteinUhlenbeckProcessOwner = owner;
}
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (QuantLib::Handle<QuantLib::OrnsteinUhlenbeckProcess> childNative)  : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
	_phOrnsteinUhlenbeckProcess = &childNative;
	_ppOrnsteinUhlenbeckProcess = &static_cast<boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::COrnsteinUhlenbeckProcess (QuantLib::OrnsteinUhlenbeckProcess childNative)  : CStochasticProcess1D(COrnsteinUhlenbeckProcess::typeid)
{
#ifdef HANDLE
	_phOrnsteinUhlenbeckProcess = NULL;
#endif
	_ppOrnsteinUhlenbeckProcess = new boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess> (new QuantLib::OrnsteinUhlenbeckProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppOrnsteinUhlenbeckProcess));
}
#endif

Cephei::QL::Processes::COrnsteinUhlenbeckProcess::~COrnsteinUhlenbeckProcess ()
{
    if (_ppOrnsteinUhlenbeckProcess != NULL)
    {
	    delete _ppOrnsteinUhlenbeckProcess;
        _ppOrnsteinUhlenbeckProcess = NULL;
    }
}
Cephei::QL::Processes::COrnsteinUhlenbeckProcess::!COrnsteinUhlenbeckProcess ()
{
    if (_ppOrnsteinUhlenbeckProcess != NULL)
    {
	    delete _ppOrnsteinUhlenbeckProcess;
    }
}
QuantLib::OrnsteinUhlenbeckProcess& Cephei::QL::Processes::COrnsteinUhlenbeckProcess::GetReference ()
{
    if (_ppOrnsteinUhlenbeckProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppOrnsteinUhlenbeckProcess;
}
boost::shared_ptr<QuantLib::OrnsteinUhlenbeckProcess>& Cephei::QL::Processes::COrnsteinUhlenbeckProcess::GetShared ()
{
    if (_ppOrnsteinUhlenbeckProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppOrnsteinUhlenbeckProcess;
}
QuantLib::OrnsteinUhlenbeckProcess* Cephei::QL::Processes::COrnsteinUhlenbeckProcess::GetPointer ()
{
    if (_ppOrnsteinUhlenbeckProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppOrnsteinUhlenbeckProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OrnsteinUhlenbeckProcess>& Cephei::QL::Processes::COrnsteinUhlenbeckProcess::GetHandle ()
{
	if (_phOrnsteinUhlenbeckProcess == NULL)
	{
		_phOrnsteinUhlenbeckProcess = new Handle<QuantLib::OrnsteinUhlenbeckProcess> (*_ppOrnsteinUhlenbeckProcess);
	}
	return *_phOrnsteinUhlenbeckProcess;
}
#endif
bool Cephei::QL::Processes::COrnsteinUhlenbeckProcess::HasNative () 
{
	return (_ppOrnsteinUhlenbeckProcess != NULL);
}

Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Expectation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->expectation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Level::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->level ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Speed::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->speed ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::StdDeviation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->stdDeviation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Variance (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0); //a
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->variance ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::Volatility::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->volatility ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::COrnsteinUhlenbeckProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOrnsteinUhlenbeckProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IOrnsteinUhlenbeckProcess^ Cephei::QL::Processes::COrnsteinUhlenbeckProcess_Factory::Create (Double speed, Double vol, Microsoft::FSharp::Core::FSharpOption<Double>^ x0, Microsoft::FSharp::Core::FSharpOption<Double>^ level)
{
    return REFNEW COrnsteinUhlenbeckProcess ( speed,  vol,  x0,  level);
}
